const { merge } = require('webpack-merge')

// 引入公共配置
const common = require('./webpack.common.js')

const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')

// 合并配置
module.exports = merge(common, {
  mode: 'production',

  optimization: {
    // 代码分离
    splitChunks: {
      chunks: 'all',
      automaticNameDelimiter: '.'
    }
  },

  // source-map 模式
  devtool: 'nosource-module-source-map',

  plugins: [
    // 打包前清除旧的 dist 目录
    new CleanWebpackPlugin(),

    // 拷贝静态文件
    new CopyWebpackPlugin({
      patterns: [{ from: './public' }]
    }),

    // 压缩 css
    new OptimizeCssAssetsPlugin()
  ]
})
